﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using QuanLyTiemVang.DTO;

namespace QuanLyTiemVang.DAO
{
    class KhachHangDAO
    {
        private static QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();

        static public KHACHHANG SelectKhachHangByMaKhachHang(KHACHHANG kh)
        {
            KHACHHANG query = db.KHACHHANGs.FirstOrDefault(khachhang => khachhang.MaKhachHang == kh.MaKhachHang);
            if (query != null)
            {
                return query;
            }
            return null;
        }
        static public List<KHACHHANG> SelectKhachHangLikeTenKhachHang(KHACHHANG kh)
        {
            var query = from d in db.KHACHHANGs
                        where d.TenKhachHang.Contains(kh.TenKhachHang) == true
                        select d;
            if (query.Count() > 0)
                return query.ToList();
            return null;
        }
        static public List<KHACHHANG> SelectKhachHangLikeLoaiKhachHang(KHACHHANG kh)
        {
            var query = from d in db.KHACHHANGs
                        where kh.MaLoaiKhachHang==d.MaLoaiKhachHang
                        select d;
            if (query.Count() > 0)
                return query.ToList();
            return null;
        }
        static public List<KHACHHANG> KhachHangLikeAddress(KHACHHANG kh)
        {
            var query = from d in db.KHACHHANGs
                        where d.DiaChi.Contains(kh.DiaChi)
                        select d;
            if (query.Count() > 0)
                return query.ToList();
            return null;
        }
        static public List<KHACHHANG> KhachHangByDebt(int tiennomin = -1, int tiennomax = -1)
        {
            var query = from d in db.KHACHHANGs
                        select d;

            if (tiennomin != -1)
            {
                query = query.Where(m => m.TienNoHienTai >= tiennomin);
            }
            if (tiennomax != -1)
            {
                query = query.Where(m => m.TienNoHienTai <= tiennomax);
            }

            if (query.Count() > 0)
                return query.ToList();
            return null;
        }
        static public List<KHACHHANG> KhachHangLikeId(KHACHHANG kh)
        {
            return (from d in db.KHACHHANGs 
                    where d.MaKhachHang==kh.MaKhachHang
                    select d).ToList();
        }
        static public List<KHACHHANG> GetAllKhachHang()
        {
            var query = db.KHACHHANGs.ToList();
            if (query.Count() > 0)
                return query;
            return null;
        }
        public static void Insert(KHACHHANG kh)
        {
            db.KHACHHANGs.Add(kh);
            db.SaveChanges();
        }
        public static void Delete(KHACHHANG kh)
        {
            var query = db.KHACHHANGs.Find(kh.MaKhachHang);
            if (query != null)
            {
                db.KHACHHANGs.Remove(query);
                db.SaveChanges();
            }
        }
        public static void Update(KHACHHANG kh)
        {
            // dia chi loai khach ten khach
            var query = db.KHACHHANGs.Find(kh.MaKhachHang);
            if (query!=null)
            {
                query.DiaChi = kh.DiaChi;
                query.MaLoaiKhachHang = kh.MaLoaiKhachHang;
                query.TenKhachHang = kh.TenKhachHang;
                db.SaveChanges();
            }
        }
        public static void UpdateTienNo(KHACHHANG kh)
        {
            var query = db.KHACHHANGs.Find(kh.MaKhachHang);
            if (query != null)
            {
                query.TienNoHienTai = kh.TienNoHienTai;
                db.SaveChanges();
            }
        }
        public static List<LOAIKHACHHANG> GetAllLoaiKhachHang()
        {
            var query = db.LOAIKHACHHANGs.ToList();
            if (query.Count > 0)
                return query;
            return null;
        }

        // news Tam
        public static List<int> GetAllKhachHangId()
        {
            var query = from kh in db.KHACHHANGs
                        select kh.MaKhachHang;
            
            return query.ToList();
        }

        // news Quy
        public static List<int> GetAllKhachQuenId()
        {
            var query = from d in db.KHACHHANGs
                        where d.LOAIKHACHHANG.TenLoaiKhachHang.Contains("Khách quen")
                        select d.MaKhachHang;

            return query.ToList();
        }

        static public KHACHHANG SelectKhachHangByMaKhachHang(int makh)
        {
            var query = from d in db.KHACHHANGs
                        where d.MaKhachHang == makh
                        select d;

            if (query.Count() == 0)
            {
                return null;
            }

            return query.First();
        }

        // new Thien
        public static List<String> GetAllTenLoaiKhachHang()
        {
            var query = db.LOAIKHACHHANGs.ToList();
            if (query.Count > 0)
            {
                List<String> lstTen = new List<String>();
                foreach (var item in query)
                {
                    lstTen.Add(item.TenLoaiKhachHang);
                }
                return lstTen;
            }
            return null;
        }

        public static int GetLastMaKhachHang()
        {
            var query = (from d in db.KHACHHANGs
                         select d.MaKhachHang).ToList();

            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
